﻿using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;

namespace Ommf
{
    public class Batch
    {
        private Image image;

        internal Batch(Image image)
        {
            this.image = image;
        }

        public void Begin(float baseX, float baseY, float angle, float scaleX, float scaleY)
        {
            Gl.glColor4ub(255, 255, 255, 255);
            image.Bind();
            
            Gl.glPushMatrix();

            Gl.glTranslatef(baseX, baseY, 0);
            Gl.glRotatef(angle, 0, 0, 1);
            Gl.glScalef(scaleX, scaleY, 1.0f);
            Gl.glTranslatef(-image.CenterX, -image.CenterY, 0);

            Gl.glBegin(Gl.GL_QUADS);
        }

        public void Draw(float x, float y)
        {
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(x, y, 0);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(x, y + image.Height, 0);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3f(x + image.Width, y + image.Height, 0);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(x + image.Width, y, 0);
        }

        public void Draw(float x, float y, float subX, float subY, float subWidth, float subHeight)
        {
            float tX1 = subX / image.Width;
            float tY1 = subY / image.Height;
            float tX2 = (subX + subWidth) / image.Width;
            float tY2 = (subY + subHeight) / image.Height;

            Gl.glTexCoord2f(tX1, tY1); Gl.glVertex3f(x, y, 0);
            Gl.glTexCoord2f(tX1, tY2); Gl.glVertex3f(x, y + subHeight, 0);
            Gl.glTexCoord2f(tX2, tY2); Gl.glVertex3f(x + subWidth, y + subHeight, 0);
            Gl.glTexCoord2f(tX2, tY1); Gl.glVertex3f(x + subWidth, y, 0);
        }

        public void End()
        {
            Gl.glEnd();
            Gl.glPopMatrix();
        }
    }
}
